import { createSlice ,PayloadAction } from "@reduxjs/toolkit";

export type userStateType = {
    userName : string,
    nickName : string
}

const INIT_STATE :userStateType = {
    userName : '',
    nickName : ''
}

export const userSlice = createSlice({
    name : 'user',
    initialState : INIT_STATE,
    reducers :{
        //登陆时，存储用户信息
        loginReducer : (state : userStateType , action:PayloadAction<userStateType>) => {
            return action.payload
        },
        // 推出时，清空用户信息
        logOutReducer : () => INIT_STATE
    }
})

// 导出 redux toolkits中自动封装好的action对象
export const {loginReducer ,logOutReducer } = userSlice.actions
export default userSlice.reducer
